package com.inno.subject.common.enums;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Pair;
import com.inno.subject.common.utils.DateUtils;
import lombok.Getter;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author shenzeyang
 * @Description: 答题正确次数奖励枚举
 */
@Getter
public enum CorrectCountRewardEnum {
    /**
     * 累计答对10题 一天vip
     */
    ONE_DAY_VIP(10L, 1,MemberTypeEnum.DAY),
    /**
     * 累计答对 50题 一周vip
     */
    ONE_WEEK_VIP(50L, 1,MemberTypeEnum.DAY),

    /**
     * 累计答对100题 一个月vip
     */
    ONE_MONTH_VIP(100L, 1,MemberTypeEnum.MONTH),

    /**
     * 累计答对500题 一年vip
     */
    ONE_YEAR_VIP(500L, 1,MemberTypeEnum.YEAR);

    private final Long correctCount;

    private final Integer rewardDays;
    private final MemberTypeEnum memberTypeEnum;

    CorrectCountRewardEnum(Long correctCount, Integer rewardDays,MemberTypeEnum memberTypeEnum) {
        this.correctCount = correctCount;
        this.rewardDays = rewardDays;
        this.memberTypeEnum = memberTypeEnum;
    }

    public static CorrectCountRewardEnum getByCorrectCount(Long correctCount) {
        for (CorrectCountRewardEnum value : CorrectCountRewardEnum.values()) {
            if (value.getCorrectCount().equals(correctCount)) {
                return value;
            }
        }
        return null;
    }

    /**
     * 根据传入的正确答题次数计算会员结束时间
     * 返回Pair 第一个元素为到期时间 第二个元素为会员类型
     */
    public static Pair<Date, MemberTypeEnum> calcMemberEndTimeAndMemberType(Long correctCount) {
        Assert.notNull(correctCount, "cannot be calculation because : correctCount is null");
        Date nowDate = DateUtils.getNowDate();
        Pair<Date, MemberTypeEnum> pair ;

        // 使用if else判断上面的三元表达式
        if (correctCount >= ONE_DAY_VIP.getCorrectCount() && correctCount < ONE_WEEK_VIP.getCorrectCount()){
            pair = Pair.of(DateUtils.addDays(nowDate, ONE_DAY_VIP.getRewardDays()),ONE_DAY_VIP.getMemberTypeEnum());
        }else if (correctCount >= ONE_WEEK_VIP.getCorrectCount() && correctCount < ONE_MONTH_VIP.getCorrectCount()){
            pair = Pair.of(DateUtils.addWeeks(nowDate, ONE_WEEK_VIP.getRewardDays()),ONE_WEEK_VIP.getMemberTypeEnum());
        }else if (correctCount >= ONE_MONTH_VIP.getCorrectCount() && correctCount < ONE_YEAR_VIP.getCorrectCount()){
            pair = Pair.of(DateUtils.addMonths(nowDate, ONE_MONTH_VIP.getRewardDays()),ONE_MONTH_VIP.getMemberTypeEnum());
        }else {
            pair = Pair.of(DateUtils.addYears(nowDate, ONE_YEAR_VIP.getRewardDays()),ONE_YEAR_VIP.getMemberTypeEnum());
        }
        return pair;

    }

}
